package com.yl.chainStore.payrule.item.entity

import com.yl.chainStore.common.Constants
import com.yl.chainStore.entity.pay.PayCustomerOrder
import com.yl.chainStore.payrule.common.RuleField
import com.yl.chainStore.payrule.item.ItemRule
import com.yl.chainStore.payrule.item.UseItemForSelectRule
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import java.math.BigDecimal
import java.math.RoundingMode

/**
 * 价格抵扣
 */
data class MoneyToMoneyTicketRule(
	/**
	 * 项目id
	 */
	@RuleField("购买商品","pay/item/getItemSelect", Constants.TableColumn.TABLE_COLUMN_SELECT_TRANSVERSE_QUERY)
	val itemId: Long,

	/**
	 * 抵用多少钱
	 */
	var disCountMoney: Long

) : ItemRule, UseItemForSelectRule() {


	override fun validationSelf(): ResponseBody {
		return returnSuccess()
	}

	override fun canUseForOrder(order: PayCustomerOrder): Boolean {
		if(!order.details.isNullOrEmpty()){
			for(detail in order.details!!){
				if(detail.itemId == itemId){
					return getUseForOrderDetails(order).isNotEmpty()
				}
			}
		}
		return false
	}

	/**
	 * 折扣金额
	 */
	@RuleField(viewName = "抵用金额")
	var disCountMoneyShow: BigDecimal?= null
		get() {
			if (field == null) {
				return BigDecimal(disCountMoney.div(Constants.OtherConfig.MONEY_TO_DB_DOUBLE)).setScale(2, RoundingMode.HALF_UP)
			}
			return field
		}
		set(value) {
			if (value != null) {
				disCountMoney = (value.multiply(BigDecimal(Constants.OtherConfig.MONEY_TO_DB_DOUBLE))).toLong()
				field = value
			}
		}

}
